package com.jilinxiangyun;

import com.alibaba.jvm.sandbox.api.Information;
import com.alibaba.jvm.sandbox.api.Module;
import com.alibaba.jvm.sandbox.api.ProcessController;
import com.alibaba.jvm.sandbox.api.annotation.Command;
import com.alibaba.jvm.sandbox.api.listener.ext.Advice;
import com.alibaba.jvm.sandbox.api.listener.ext.AdviceListener;
import com.alibaba.jvm.sandbox.api.listener.ext.EventWatchBuilder;
import com.alibaba.jvm.sandbox.api.resource.ModuleEventWatcher;
import org.kohsuke.MetaInfServices;

import javax.annotation.Resource;

/**
 * Description：
 *
 * @author GavinHacker
 * @since Created in 上午10:20 2024/1/9
 */
@MetaInfServices(Module.class)
@Information(id = "sjzd-debug-2")
public class SjzdDebug2 implements Module {

    @Resource
    private ModuleEventWatcher moduleEventWatcher;

    @Command("sjzd-debug-2-cmd")
    public void debug() {

        new EventWatchBuilder(moduleEventWatcher)
                .onClass("com.ruoyi.web.controller.system.SysUserController")
                .onBehavior("list")
                .onWatch(new AdviceListener() {


                    @Override
                    protected void afterReturning(Advice advice) throws Throwable {
                        Object[] slots = advice.getParameterArray();
                        // 遍历局部变量表，获取变量值
                        for (Object slot : slots) {
                            System.out.print("X:" + slot);
                        }
                        System.out.println("$$$$$$$$$$$$$$$$$$");
                    }

                    @Override
                    protected void before(Advice advice) throws Throwable {
                        Object[] slots = advice.getParameterArray();
                        System.out.println("**********************8");
                        throw new Exception();
                        //super.before(advice);
                    }

                    @Override
                    protected void afterThrowing(Advice advice) throws Throwable {
                        ProcessController.returnImmediately(null);
                    }

                    @Override
                    protected void beforeLine(Advice advice, int lineNum) {
                        System.out.println("#########################");
                        Object[] slots = advice.getParameterArray();
                        // 遍历局部变量表，获取变量值
                        for (Object slot : slots) {
                            System.out.print("X:" + slot);
                        }
                        super.beforeLine(advice, 11);
                        slots = advice.getParameterArray();
                        // 遍历局部变量表，获取变量值
                        for (Object slot : slots) {
                            System.out.print("X:" + slot);
                        }
                        System.out.println("#########################2");
                    }
                });
    }
}